//厦门大学计算机专业 | 前华为工程师
//专注《零基础学编程系列》 http://lblbc.cn/blog
//包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
//公众号：蓝不蓝编程
<template>
	<view class="page-wrapper">
		<view class="search_wrapper">
			<view class="search_row">
				<image class="search-img" src="/static/images/search.png"></image>
				<input class="search-input" placeholder="点击搜索" @input="_bindinput" />
			</view>
			<view class="search-text" :auto-focus="true" @tap="searchGoods">搜索</view>
		</view>

		<view class="goods-row" @tap="gotoGoodsDetail" :data-id="item.id" v-for="(item, index) in goodsList"
			:key="item.k">
			<image class="goods-img" :src="item.squarePic"></image>

			<view class="goods-desc">
				<view class="goods-name">{{ item.name }}</view>
				<view class="goods-price">￥{{ item.price / 100 }}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		search
	} from '@/common/api'
	export default {
		data() {
			return {
				goodsList: [],
				keyword: ''
			};
		},
		methods: {
			_bindinput(e) {
				this.setData({
					keyword: e.detail.value
				});
			},

			searchGoods() {
				let _this = this;
				search(this.keyword).then(resp => {
					if (!resp.data.code) {
						this.goodsList = resp.data.data
					}
				})
			},

			gotoGoodsDetail(e) {
				let goodsId = e.currentTarget.dataset['id'];
				uni.navigateTo({
					url: '/pages/goods/goods?id=' + goodsId
				});
			}
		}
	};
</script>
<style>
	page {
		background-color: #f4f4f4;
	}

	.page-wrapper {
		margin: 0;
		font-size: 28rpx;
		line-height: 1.5;
		color: #333;
		overflow-x: hidden;
		-webkit-overflow-scrolling: touch;
		-webkit-tap-highlight-color: transparent;
	}

	.search_wrapper {
		display: flex;
		flex-direction: row;
		background-color: #ef3965;
		padding-bottom: 20rpx;
		align-items: center;
	}

	.search_row {
		display: flex;
		flex-direction: row;
		flex: 1;
		background-color: white;
		border-radius: 10rem;
		margin-left: 1rem;
		margin-right: 1rem;
		padding-top: 10rpx;
		padding-bottom: 10rpx;
		align-items: center;
	}

	.search-img {
		height: 40rpx;
		width: 40rpx;
		margin-left: 20rpx;
	}

	.search-text {
		color: #fff;
		margin-right: 1rem;
	}

	.search-input {
		margin-left: 20rpx;
	}

	.goods-row {
		display: flex;
		flex-direction: row;
		background-color: #fff;
		border-radius: 10rpx;
		margin-top: 20rpx;
		margin-left: 20rpx;
		margin-right: 20rpx;
		padding: 20rpx;
	}

	.goods-img {
		margin-left: 40rpx;
		width: 200rpx;
		height: 200rpx;
	}

	.goods-desc {
		display: flex;
		flex-direction: column;
		margin-left: 40rpx;
	}

	.goods-name {
		font-size: 35rpx;
		width: 400rpx;
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		word-wrap: break-word;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.goods-price {
		margin-top: 20rpx;
		color: #ef3965;
	}
</style>
